﻿using System.Collections.Generic;
using System.Configuration;
using System.Windows;

namespace BabyCloudDrives.DesktopApp.Utilities
{
    public static class PresetValues
    {
        private static readonly IDictionary<string, string> _allResponses = new Dictionary<string, string>();
        private static readonly ResponseValueClass _responseValue;
        public const string ClientId = "000000004C0C3A0F";

        public static IDictionary<string, string> AllResponses
        {
            get { return _allResponses; }
        }

        public static ResponseValueClass ResponseValue
        {
            get { return _responseValue; }
        }

        static PresetValues()
        {
            _responseValue = new ResponseValueClass();
            ResponseValue.AccessToken = ConfigurationManager.AppSettings[ResponseKey.AccessToken];
        }

        public class ResponseValueClass : DependencyObject
        {
            public static readonly DependencyProperty AccessTokenProperty =
                DependencyProperty.Register("AccessToken", typeof (object), typeof (ResponseValueClass), null);

            public string AccessToken
            {
                get
                {
                    if (AllResponses.ContainsKey(ResponseKey.AccessToken) &&
                        !string.IsNullOrEmpty(ResponseKey.AccessToken))
                        return AllResponses[ResponseKey.AccessToken];
                    return GetValue(AccessTokenProperty) == null
                               ? ConfigurationManager.AppSettings[ResponseKey.AccessToken]
                               : GetValue(AccessTokenProperty).ToString();
                }
                set
                {
                    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                    config.AppSettings.Settings.Add(ResponseKey.AccessToken, value);
                    AllResponses[ResponseKey.AccessToken] = value;
                    config.Save(ConfigurationSaveMode.Modified);
                    ConfigurationManager.RefreshSection("appSettings");
                    SetValue(AccessTokenProperty, value);
                }
            }
        }

        public abstract class ResponseKey
        {
            public const string AccessToken = "access_token";
            public const string RefreshToken = "refresh_token";
            public const string Error = "error";
        }
    }
}